package com.itheima.pinkman.service.impl;

import com.itheima.pinkman.mapper.RegistryMapper;
import com.itheima.pinkman.pojo.ServiceInfo;
import com.itheima.pinkman.pojo.TransferServiceInfo;
import com.itheima.pinkman.service.RegistryService;
import jdk.nashorn.internal.ir.IfNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Slf4j
public class RegistryServiceImpl implements RegistryService {
    @Autowired(required = false)
    RegistryMapper registryMapper;

    @Override
    public void add(ServiceInfo serviceInfo) {
        serviceInfo.setServiceId("NEW UP");
        //非空判断
        if (serviceInfo!=null) {
            List<ServiceInfo> serviceInfoList = registryMapper.findByAppName(serviceInfo.getAppName());
            if (serviceInfoList==null){
                //第一次注册
                //参数严谨性判断
                if (serviceInfo.getPort()>65535 || serviceInfo.getAppName().length()>50){
                    return;
                }

                registryMapper.insertSelective(serviceInfo);
            }else {
                //不是第一次注册,先删在注册
                for (ServiceInfo info : serviceInfoList) {
                    if (info.getIp().equals(serviceInfo.getIp())&&info.getPort()==serviceInfo.getPort()){
                        registryMapper.deleteByAppName(serviceInfo.getAppName());
                    }
                }
                registryMapper.insertSelective(serviceInfo);
            }
        }

    }

    @Override
    public void delete(ServiceInfo serviceInfo) {
        registryMapper.deleteByExample(serviceInfo.getIp());
    }

    /**
     * 接收心跳,更新updateTime
     * @param serviceInfo
     */
    @Override
    public void update(ServiceInfo serviceInfo) {
        //收到心跳更改状态
        serviceInfo.setServiceId("RENEW UP");
        registryMapper.updateByRenew(serviceInfo);
//        System.out.println(new Date()+"收到心跳");
        log.info(new Date()+"收到心跳");
    }

    @Override
    public List<ServiceInfo> findAll() {
        return registryMapper.selectAll();
    }


    @Override
    public List<TransferServiceInfo> findList() {
        //新建一个List<ServiceInfo>接收转换过后的数组
        List<TransferServiceInfo> transferServiceInfoList=new ArrayList<TransferServiceInfo>();

        //把更新时间转换为yyyy-MM-dd HH:mm:ss格式
        List<ServiceInfo> serviceInfoList = registryMapper.selectAll();
        for (ServiceInfo serviceInfo : serviceInfoList) {
            TransferServiceInfo transferServiceInfo = new TransferServiceInfo();
            long updateTime = serviceInfo.getUpdateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String upTime = simpleDateFormat.format(updateTime);
            transferServiceInfo.setUpdateTime(upTime);

            transferServiceInfo.setAppName(serviceInfo.getAppName());
            transferServiceInfo.setIp(serviceInfo.getIp());
            transferServiceInfo.setPort(serviceInfo.getPort());
            transferServiceInfo.setServiceId(serviceInfo.getServiceId());
            transferServiceInfo.setCreateTime(serviceInfo.getCreateTime());
            transferServiceInfoList.add(transferServiceInfo);
        }

        return transferServiceInfoList;
    }

    @Override
    public void ifNotExistCreateDb() {
        registryMapper.ifNotExistCreateDb();
    }

}
